<template>
  <el-dialog
   title="新增"
   :visible.sync="dialogVisible"
   width="20%"
   :close-on-click-modal="false"
   :before-close="handleClose">
    <el-form :model="sizeForm" ref="sizeForm" :rules="rules" label-width="100px">
      <el-form-item label="名称:" prop="sizeName">
       <el-input v-model="sizeForm.sizeName" placeholder="请输入名称"></el-input>
      </el-form-item>
      <el-form-item label="排序值:">
       <el-input v-model="sizeForm.sort" placeholder="请输入排序值"></el-input>
      </el-form-item>
      <el-form-item label="启用状态">
       <EnabledSwitch :value.sync="sizeForm.isEnabled"/>
      </el-form-item>
    </el-form>

    <span class="dialog-footer">
     <el-button @click="handleClose">取 消</el-button>
     <el-button type="primary" :loading="loading" @click="confirm">确 定</el-button>
    </span>
   </el-dialog>
</template>
<script>
import EnabledSwitch from '@/components/EnabledSwitch/index.vue'

import { goodsSizeAdd } from '@/api/goods/size'

export default {
  name: 'AddGoodsSize',
  components: {
    EnabledSwitch
  },
  props: {
    showDialog: {
      type: Boolean,
      default: ()=> false
    }
  },
  computed: {
    dialogVisible: {
      get() {
        return this.showDialog
      }
    }
  },
  data() {
    return {
      sizeForm: null,
      loading: false,
      initForm: {
        sizeName: '',
        sort: 100,
        isEnabled: 1
      },
      rules: {
        sizeName: [{ required: true, message: '请输入名称', trigger: 'blur, change' }]
      },
    }
  },
  created(){
   this.sizeForm = { ...this.initForm }
  },
  methods: {
    // 新增
    async confirm() {
      this.$refs.sizeForm.validate(async (valid) => {
       if(valid){
         try{
           this.loading = true
           await goodsSizeAdd(this.sizeForm)
           this.handleClose()
           this.$emit('reloadData')
         }catch(e){
           this.loading = false
         }
       }else {
         console.log('error submit!!');
         this.loading = false
         return false;
       }
      });
    },

    // 关闭
    handleClose() {
      this.$emit('update:showDialog', false)
      this.loading = false
      this.sizeForm = { ...this.initForm }
      this.$refs.sizeForm.resetFields()
    }
  }
}
</script>